package popcorn.ticket.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.entity.Grades;
import popcorn.ticket.entity.Orders;
import popcorn.ticket.serviceimpl.ScheduleServiceImpl;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.MH1;
import popcorn.ticket.tool.MH2;
import popcorn.ticket.tool.MovieSche;
import popcorn.ticket.tool.ScheInfo;
import popcorn.ticket.tool.SeatInfo;

@RestController
public class ScheduleController {
	@Autowired
	ScheduleServiceImpl ssi;

	/**
	 * 获取影厅信息
	 * 
	 * @return
	 */
	@PostMapping(value = "getMH1", produces = "application/json;charset=utf-8")
	public List<MH1> getMH1() {
		return ssi.getMH1();
	}

	/**
	 * 获取电影名
	 * 
	 * @return
	 */
	@PostMapping(value = "getMH2", produces = "application/json;charset=utf-8")
	public List<MH2> getMH2() {
		return ssi.getMH2();
	}

	/**
	 * 获取排期信息
	 * 
	 * @return
	 */
	@GetMapping(value = "getMoviceSche", produces = "application/json;charset=utf-8")
	public List<MovieSche> getMoviceSche() {
		return ssi.getSchedule();
	}

	/**
	 * 添加/修改排期信息
	 * 
	 * @return
	 */
	@PostMapping(value = "addOrUpdSche", produces = "application/json;charset=utf-8", consumes = "application/json")
	public AjaxResponse addOrUpdSche(@RequestBody MovieSche ms) {

		int scheduleid=ms.getAuto();
		int code =0;
		String msg ="";
		//判断是否已存在此排期
		if (scheduleid!=0) {
			//修改
			int n = ssi.updSche(ms);
		    code = 1;
		    msg = "修改成功！";
			if (n == 0) {
				code = 0;
				msg = "修改失败！";
			}
		
		}else {
			//增加
			int n = ssi.addSchedule(ms);
			 code = 1;
			 msg = "添加成功！";
			if (n == 0) {
				code = 0;
				msg = "添加失败！";
			}

		}	
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
	
	/**
	 * 删除排期信息
	 * 
	 * @return
	 */
	@GetMapping(value = "delSche/{scheduleid}", produces = "application/json;charset=utf-8")
	public AjaxResponse delSche(@PathVariable(value = "scheduleid")int scheduleid) {

		int code =1;
		String msg ="删除成功！";
		int n = ssi.delSche(scheduleid);
		if (n == 0) {
				code = 0;
				msg = "修改失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
	
	/**
	 * 前台页面需要的排期数据
	 */
	@GetMapping(value ="getScheInfo",produces = "application/json;charset=utf-8")
	public ScheInfo  getScheInfo(String time,int movieid){
		ScheInfo si =ssi.getInfof(time, movieid);

		return si;
	}
	/**
	 * 选座页面所需要的数据
	 */
	@GetMapping(value ="getSeatInfo",produces = "application/json;charset=utf-8")
	public SeatInfo  getSeatInfo(int scheduleid,int movieid){
		SeatInfo si=ssi.getInfoj(movieid, scheduleid);

		return si;
	}
	
	/**
	 * 获取userid对应的diacountLevel
	 */
	@GetMapping(value ="getDiscountLevel",produces = "application/json;charset=utf-8")
	public Grades getDiscountLevel(int userid) {
		return ssi.getDiscoutLevel(userid);
	}
	
	/**
	 * 添加订单
	 */
	@PostMapping(value = "addOrderss", produces = "application/json;charset=utf-8", consumes = "application/json")
	public AjaxResponse addOrderss(@RequestBody Orders orders) {
		orders.setOrdertime(new Date());
		orders.setOrdercondition(1);
		orders.setOrderstatus(2);
		int code =1;
		String msg ="添加成功！";
		int n = ssi.addOrders(orders);
		if (n == 0) {
				code = 0;
				msg = "添加失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
	
}
